package com.hq.ims.data.event.listener;

import com.hq.ims.data.entity.*;
import com.hq.ims.data.entityenum.EnumForewarningInfoClassify;
import com.hq.ims.data.entityenum.EnumUserTodoType;
import com.hq.ims.data.event.BusInfoNotificationEvent;
import com.hq.ims.data.service.BaseBusInfoForewarningService;
import com.hq.ims.data.service.BaseBusInfoTrackService;
import com.hq.ims.data.service.BaseBusiInfoService;
import com.hq.ims.data.service.BaseUserTodoStatisticsService;
import com.hq.ims.data.vo.BusInfoForewarningMapstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.transaction.event.TransactionalEventListener;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户跟踪反馈事件
 *
 * @date 2022/6/7 15:41
 */
@Component
@Slf4j
public class BusInfoNotificationListener {

    @Resource
    private BaseUserTodoStatisticsService baseUserTodoStatisticsService;
    @Resource
    private BaseBusiInfoService baseBusiInfoService;
    @Resource
    private BaseBusInfoTrackService baseBusInfoTrackService;
    @Resource
    private BaseBusInfoForewarningService baseBusInfoForewarningService;

    @Async
    @TransactionalEventListener
    public void onApplicationEvent(BusInfoNotificationEvent busInfoNotificationEvent) {
        String eventType = busInfoNotificationEvent.getEventType();

        switch (eventType) {
            case BusInfoNotificationEvent.COMPANY_RECEIVE_EVENT:
                this.companyReceiveEvent(busInfoNotificationEvent);
                break;
            case BusInfoNotificationEvent.COMPANY_REJECTION_EVENT:
                this.companyRejectionEvent(busInfoNotificationEvent);
                break;
            case BusInfoNotificationEvent.PUSH_TO_COMPANY:
                this.pushToCompany(busInfoNotificationEvent);
                break;
            default:
                break;
        }
    }

    /**
     * 公司拒收事件
     */
    public void companyRejectionEvent(BusInfoNotificationEvent busInfoNotificationEvent) {
        //用户端给发布人+1
        BusInfoNotification busInfoNotification = busInfoNotificationEvent.getBusInfoNotification();
        BusiInfo busiInfo = baseBusiInfoService.getById(busInfoNotificationEvent.getBusInfoNotification().getBusiInfoId());
        if (busiInfo == null) {
            return;
        }

        //给发布者一个通知待办
        baseUserTodoStatisticsService.updateCountByUserTodoType(
                EnumUserTodoType.BusinfoReceiveNotifyCount,
                1,
                busInfoNotificationEvent.getBusInfoNotification().getBusiInfoId(),
                busiInfo.getPublisherId(),
                busiInfo.getOrgId()
        );

        // 移除推送到公司的未接收预警
        baseBusInfoForewarningService.removeForewarning(busInfoNotification.getBusInfoNotificationId());
    }

    /**
     * 公司接收事件
     */
    public void companyReceiveEvent(BusInfoNotificationEvent busInfoNotificationEvent) {
        BusInfoNotification busInfoNotification = busInfoNotificationEvent.getBusInfoNotification();
        // 查询当前接收经营信息的产业标记的预警数据
        List<BusInfoForewarning> infoForewarningList = baseBusInfoForewarningService.lambdaQuery()
                .select(BusInfoForewarning::getDataId)
                .eq(BusInfoForewarning::getBusiInfoId, busInfoNotification.getBusiInfoId())
                .eq(BusInfoForewarning::getForewarningInfoClassify, EnumForewarningInfoClassify.NotReceiveForewarning)
                .eq(BusInfoForewarning::getIndustryMarkId, busInfoNotification.getIndustryMarkId())
                .list();
        // 移除已接收的经营信息的"未接收"预警
        infoForewarningList.forEach(infoForewarning -> baseBusInfoForewarningService.removeForewarning(infoForewarning.getDataId()));

        // 查询跟踪信息
        BusInfoTrack busInfoTrack = baseBusInfoTrackService.lambdaQuery()
                .eq(BusInfoTrack::getBusInfoNotificationId, busInfoNotification.getBusInfoNotificationId())
                .one();
        if (busInfoTrack != null) {
            // 添加未反馈预警(反馈阶段默认为"未接洽")
            BusInfoFeedback busInfoFeedback = new BusInfoFeedback();
            busInfoFeedback.setBusiInfoId(busInfoTrack.getBusiInfoId());
            busInfoFeedback.setBusInfoTrackId(busInfoTrack.getBusInfoTrackId());
            busInfoFeedback.setIndustryMarkId(busInfoTrack.getIndustryMarkId());
            busInfoFeedback.setIndustryMarkName(busInfoTrack.getIndustryMarkName());
            busInfoFeedback.setFeedbackPhase(busInfoTrack.getLastNewFeedbackPhase());
            busInfoFeedback.setFeedbackDate(busInfoTrack.getLastNewFeedbackDate());
            busInfoFeedback.setBusiPersonId(busInfoTrack.getBusiPersonId());
            busInfoFeedback.setBusiPersonName(busInfoTrack.getBusiPersonName());
            BusInfoForewarningMapstruct.AddForewarningVo addForewarningVo = new BusInfoForewarningMapstruct.AddForewarningVo();
            addForewarningVo.setBusInfoFeedback(busInfoFeedback);
            addForewarningVo.setForewarningInfoClassify(EnumForewarningInfoClassify.NotFeedbackForewarning);
            baseBusInfoForewarningService.addForewarning(addForewarningVo);
        }
    }

    /**
     * 推送到公司处理
     */
    public void pushToCompany(BusInfoNotificationEvent busInfoNotificationEvent) {
        // 添加未接收预警
        BusInfoForewarningMapstruct.AddForewarningVo addForewarningVo = new BusInfoForewarningMapstruct.AddForewarningVo();
        addForewarningVo.setForewarningInfoClassify(EnumForewarningInfoClassify.NotReceiveForewarning);
        addForewarningVo.setBusInfoNotification(busInfoNotificationEvent.getBusInfoNotification());
        baseBusInfoForewarningService.addForewarning(addForewarningVo);
    }
}
